{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import zipfile\n",
    "import random\n",
    "import shutil\n",
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "from tensorflow.keras.optimizers import RMSprop\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "from shutil import copyfile\n",
    "from os import getcwd\n",
    "from os import listdir\n",
    "import cv2\n",
    "from tensorflow.keras.layers import Conv2D, Input, ZeroPadding2D, BatchNormalization, Activation, MaxPooling2D, Flatten, Dense\n",
    "from tensorflow.keras.models import Model, load_model, Sequential \n",
    "from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.utils import shuffle\n",
    "import imutils\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image  as mpimg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "患病样本图片数量: 75\n",
      "健康样本图片数量: 94\n"
     ]
    }
   ],
   "source": [
    "print(\"患病样本图片数量:\",len(os.listdir('fix-csv-data/AD')))\n",
    "print(\"健康样本图片数量:\",len(os.listdir('fix-csv-data/HC')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本总数: 169\n",
      "患病样本比例: 44.37869822485207%, 患病样本数: 75\n",
      "健康样本比例: 55.62130177514793%, 健康样本数: 94\n"
     ]
    }
   ],
   "source": [
    "#数据总览\n",
    "def data_summary(main_path):\n",
    "    \n",
    "    yes_path = main_path+'AD'\n",
    "    no_path = main_path+'HC'\n",
    "        \n",
    "    # number of files (images) that are in the the folder named 'yes' that represent tumorous (positive) examples\n",
    "    m_pos = len(listdir(yes_path))\n",
    "    # number of files (images) that are in the the folder named 'no' that represent non-tumorous (negative) examples\n",
    "    m_neg = len(listdir(no_path))\n",
    "    # number of all examples\n",
    "    m = (m_pos+m_neg)\n",
    "    \n",
    "    pos_prec = (m_pos* 100.0)/ m\n",
    "    neg_prec = (m_neg* 100.0)/ m\n",
    "    \n",
    "    print(f\"样本总数: {m}\")\n",
    "    print(f\"患病样本比例: {pos_prec}%, 患病样本数: {m_pos}\") \n",
    "    print(f\"健康样本比例: {neg_prec}%, 健康样本数: {m_neg}\") \n",
    "    \n",
    "augmented_data_path = 'fix-csv-data/'    \n",
    "data_summary(augmented_data_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集中患病样本数: 37\n",
      "测试集中患病样本数: 38\n",
      "训练集中健康样本数: 47\n",
      "测试集中健康样本数: 47\n"
     ]
    }
   ],
   "source": [
    "#将数据集按比例划分为训练集和测试集\n",
    "def split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):\n",
    "    dataset = []\n",
    "    \n",
    "    for unitData in os.listdir(SOURCE):\n",
    "        data = SOURCE + unitData\n",
    "        if(os.path.getsize(data) > 0):\n",
    "            dataset.append(unitData)\n",
    "        else:\n",
    "            print('Skipped ' + unitData)\n",
    "            print('Invalid file i.e zero size')\n",
    "    \n",
    "    train_set_length = int(len(dataset) * SPLIT_SIZE)\n",
    "    test_set_length = int(len(dataset) - train_set_length)\n",
    "    shuffled_set = random.sample(dataset, len(dataset))\n",
    "    train_set = dataset[0:train_set_length]\n",
    "    test_set = dataset[-test_set_length:]\n",
    "       \n",
    "    for unitData in train_set:\n",
    "        temp_train_set = SOURCE + unitData\n",
    "        final_train_set = TRAINING + unitData\n",
    "        copyfile(temp_train_set, final_train_set)\n",
    "    \n",
    "    for unitData in test_set:\n",
    "        temp_test_set = SOURCE + unitData\n",
    "        final_test_set = TESTING + unitData\n",
    "        copyfile(temp_test_set, final_test_set)\n",
    "        \n",
    "        \n",
    "YES_SOURCE_DIR = \"fix-csv-data/AD/\"\n",
    "TRAINING_YES_DIR = \"fix-csv-data/train_ad/\"\n",
    "TESTING_YES_DIR = \"fix-csv-data/test_ad/\"\n",
    "NO_SOURCE_DIR = \"fix-csv-data/HC/\"\n",
    "TRAINING_NO_DIR = \"fix-csv-data/train_hc/\"\n",
    "TESTING_NO_DIR = \"fix-csv-data/test_hc/\"\n",
    "split_size = .5\n",
    "split_data(YES_SOURCE_DIR, TRAINING_YES_DIR, TESTING_YES_DIR, split_size)\n",
    "split_data(NO_SOURCE_DIR, TRAINING_NO_DIR, TESTING_NO_DIR, split_size)\n",
    "print(\"训练集中患病样本数:\", len(os.listdir('fix-csv-data/train_ad/')))\n",
    "print(\"测试集中患病样本数:\", len(os.listdir('fix-csv-data/test_ad/')))\n",
    "print(\"训练集中健康样本数:\", len(os.listdir('fix-csv-data/train_hc/')))\n",
    "print(\"测试集中健康样本数:\", len(os.listdir('fix-csv-data/test_hc/')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[112,  16, 112, ...,  64, 255, 128],\n",
       "       [112,  16, 176, ..., 255, 255, 255],\n",
       "       [112,  64, 112, ...,  64, 255, 255],\n",
       "       ...,\n",
       "       [112,  16, 176, ...,  64,  64, 128],\n",
       "       [176,  16, 176, ..., 255, 255, 128],\n",
       "       [112,  64, 176, ...,  16, 255,  96]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def img2vector(filename):\n",
    "    \"\"\"\n",
    "    Parameters:\n",
    "        filename - 文件名\n",
    "    Returns:\n",
    "        returnVect - 返回的二进制图像的1x29774向量\n",
    "    \"\"\"\n",
    "    fr =  pd.read_csv(filename,header=0,index_col=False,sep = ',')#从0行开始算，重新设置一列成为index值\n",
    "    returnVect=np.array(fr['feature']).reshape(1,29774 )#转换为矩阵\n",
    "    return returnVect\n",
    "#测试集的Lab\n",
    "hwLabels = []\n",
    "#返回train目录下的文件名\n",
    "train_ad_FileList = listdir('fix-csv-data/train_ad')\n",
    "train_hc_FileList = listdir('fix-csv-data/train_hc')\n",
    "#返回ad文件夹下文件的个数\n",
    "m = len(train_ad_FileList)\n",
    "#返回ad文件夹下文件的个数\n",
    "n = len(train_hc_FileList)\n",
    "#初始化训练的Mat矩阵,测试集\n",
    "trainingMat = np.zeros((m+n, 29774))\n",
    "#解析出训练集的类别\n",
    "for i in range(m):\n",
    "    #获得分类的数字\n",
    "    classNumber = int(0)\n",
    "    #获取文件名\n",
    "    fileNameStr = train_ad_FileList[i]\n",
    "    #将获得的类别添加到hwLabels中\n",
    "    hwLabels.append(classNumber)\n",
    "    #将每一个文件的1x29774数据存储到trainingMat矩阵中\n",
    "    trainingMat[i,:] = img2vector('fix-csv-data/train_ad/%s' % (fileNameStr))\n",
    "\n",
    "#解析出训练集的类别\n",
    "for i in range(n):\n",
    "    #获得分类的数字\n",
    "    classNumber = int(1)\n",
    "    #获取文件名\n",
    "    fileNameStr = train_hc_FileList[i]\n",
    "    #将获得的类别添加到hwLabels中\n",
    "    hwLabels.append(classNumber)\n",
    "    #将每一个文件的1x29774数据存储到trainingMat矩阵中\n",
    "    trainingMat[i+m,:] = img2vector('fix-csv-data/train_hc/%s' % (fileNameStr))\n",
    "hwLabels=np.array(hwLabels)#转换为矩阵\n",
    "trainingMat = trainingMat.astype('int')\n",
    "trainingMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "84"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(hwLabels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense (Dense)                (None, 512)               15244800  \n",
      "_________________________________________________________________\n",
      "activation (Activation)      (None, 512)               0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 256)               131328    \n",
      "_________________________________________________________________\n",
      "activation_1 (Activation)    (None, 256)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 128)               32896     \n",
      "_________________________________________________________________\n",
      "activation_2 (Activation)    (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 1)                 129       \n",
      "_________________________________________________________________\n",
      "activation_3 (Activation)    (None, 1)                 0         \n",
      "=================================================================\n",
      "Total params: 15,409,153\n",
      "Trainable params: 15,409,153\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "## 创建模型\n",
    "model = Sequential()\n",
    "model.add(Dense(512,input_shape=( 29774, )))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(256))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(128))\n",
    "model.add(Activation('relu'))\n",
    "model.add(Dense(1))\n",
    "model.add(Activation('sigmoid'))\n",
    "## 打印模型的概要信息\n",
    "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 1252.5723 - acc: 0.5595\n",
      "Epoch 2/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 855.2903 - acc: 0.4881\n",
      "Epoch 3/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 319.6473 - acc: 0.6429\n",
      "Epoch 4/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 8.2344 - acc: 0.9524\n",
      "Epoch 5/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 19.5698 - acc: 0.8333\n",
      "Epoch 6/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 24.2438 - acc: 0.9167\n",
      "Epoch 7/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 126.4574 - acc: 0.6786\n",
      "Epoch 8/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 300.2975 - acc: 0.5714\n",
      "Epoch 9/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 99.9007 - acc: 0.7619\n",
      "Epoch 10/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 13.5928 - acc: 0.9405\n",
      "Epoch 11/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 14.4356 - acc: 0.9643\n",
      "Epoch 12/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 3.0242 - acc: 0.9762\n",
      "Epoch 13/13\n",
      "9/9 [==============================] - 0s 6ms/step - loss: 10.5134 - acc: 0.9881\n"
     ]
    }
   ],
   "source": [
    "\n",
    "## 设置模型的训练参数\n",
    "history = model.fit(trainingMat,\n",
    "                    hwLabels,\n",
    "                    epochs=13,\n",
    "                    batch_size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [0.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.],\n",
       "       [1.]], dtype=float32)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(trainingMat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEWCAYAAABrDZDcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAebUlEQVR4nO3df5QcZZ3v8fdnEkIy/Aom0YMJmUFkoxAJhCbqJbu6/NDg8kNkjyY73AsqjqwGWa6uRsMKh0Nk9yhGvWa9jvhzaUEOrhD2IgiIuKxomEhAEyTEbBIG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a0K9r6AMl656PiDkkV5B+JS37OvCDdA76IvC1tPxrwN0RMYtkXqPVafkRwLKIOAp4Fjg7LV8EHJtu54Ksds5sIL6y2Cwl6cWI2L9M+QbgxIhYn04s+ERETJL0FHBIRGxLyzdHxGRJ3cC0iPhzyTZagdvTm7Ag6TPAPhFxhaRbgReBG4EbI+LFjHfVbBduEZhVJgZ4PVCdcv5c8voVXh2j+xtgGXAcsDK98YxZzTgRmFXmAyXP96avf8Wrt4tsA+5JX98J/D3svO/zgQNtVFITcGhE3EVyI6CJwG6tErMs+cjD7FUTJK0qWb41IvpOId1X0m9IDp4WpGWfAL4j6R9J7oL2wbT8IqAjnZXyFZKksHmAzxwDXCPpIEDA0ga+laY1KI8RmO1BOkZQiIin6h2LWRbcNWRmlnNuEZiZ5ZxbBGZmOedEYGaWc04EZmY550RgZpZzTgRmZjn3/wFLrmWjPrZajwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "history_dict = history.history\n",
    "history_dict.keys()\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "acc = history_dict['acc']\n",
    "loss = history_dict['loss']\n",
    "\n",
    "epochs = range(1, len(acc) + 1)\n",
    "# “bo”代表 \"蓝点\"\n",
    "plt.plot(epochs, acc, 'bo', label='Training acc')\n",
    "# b代表“蓝色实线”\n",
    "plt.title('Training and validation acc')\n",
    "plt.xlabel('Epochs')\n",
    "plt.ylabel('acc')\n",
    "plt.legend()\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('cnn.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.models.load_model('cnn.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件名为037_S_5162.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为053_S_5070.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为053_S_5208.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为067_S_4728.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为067_S_5205.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为070_S_4719.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为072_S_4769.csv\t分类返回结果为1\t真实结果为0\n",
      "文件名为073_S_5016.csv\t分类返回结果为1\t真实结果为0\n",
      "文件名为073_S_5090.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为100_S_5106.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为116_S_4195.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为116_S_4209.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为116_S_4338.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为116_S_4732.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为123_S_4526.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为128_S_4772.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为128_S_4774.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为128_S_4792.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为128_S_5123.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4730.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4971.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4982.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4984.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4990.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_4997.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_5006.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_5059.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为130_S_5231.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为131_S_5138.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为135_S_4863.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为135_S_4954.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为135_S_5015.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为135_S_5275.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为136_S_4993.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为137_S_4211.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为137_S_4258.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为137_S_4756.csv\t分类返回结果为0\t真实结果为0\n",
      "文件名为153_S_4172.csv\t分类返回结果为0\t真实结果为0\n",
      "总共错了2个数据\n",
      "错误率为5.263158%\n"
     ]
    }
   ],
   "source": [
    "#返回test目录下的文件列表\n",
    "testFileList = listdir('fix-csv-data/test_ad')\n",
    "#错误检测计数\n",
    "errorCount = 0.0\n",
    "#测试数据的数量\n",
    "mTest = len(testFileList)\n",
    "#从文件中解析出测试集的类别并进行分类测试\n",
    "for i in range(mTest):\n",
    "    #获得文件的名字\n",
    "    fileNameStr = testFileList[i]\n",
    "    #获得分类的数字\n",
    "    classNumber = int(0)\n",
    "    #获得测试集的1x29929向量,用于训练\n",
    "    vectorUnderTest = img2vector('fix-csv-data/test_ad/%s' % (fileNameStr))\n",
    "    #获得预测结果\n",
    "    classifierResult = model.predict(vectorUnderTest)\n",
    "    print(\"文件名为%s\\t分类返回结果为%d\\t真实结果为%d\" % (fileNameStr,classifierResult, classNumber))\n",
    "    if(classifierResult != classNumber):\n",
    "        errorCount += 1.0\n",
    "print(\"总共错了%d个数据\\n错误率为%f%%\" % (errorCount, errorCount/mTest * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件名为033_S_4508.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为036_S_4389.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为036_S_4491.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为036_S_4878.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为037_S_4028.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为037_S_4071.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为037_S_4308.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为037_S_4410.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为070_S_4856.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为070_S_5040.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4155.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4382.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4393.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4739.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4762.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_4795.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为073_S_5023.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为094_S_4234.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4010.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4043.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4092.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4453.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4483.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为116_S_4855.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为128_S_4586.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为128_S_4832.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为129_S_4369.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为129_S_4371.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为129_S_4396.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为129_S_4422.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为130_S_4343.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为130_S_4352.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为135_S_4446.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为136_S_4269.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为137_S_4466.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为137_S_4587.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为137_S_4632.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为153_S_4125.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为153_S_4139.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为153_S_4151.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为153_S_4372.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4066.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4100.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4255.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4292.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4365.csv\t分类返回结果为1\t真实结果为1\n",
      "文件名为941_S_4376.csv\t分类返回结果为1\t真实结果为1\n",
      "总共错了0个数据\n",
      "错误率为0.000000%\n"
     ]
    }
   ],
   "source": [
    "#返回test目录下的文件列表\n",
    "testFileList = listdir('fix-csv-data/test_hc')\n",
    "#错误检测计数\n",
    "errorCount = 0.0\n",
    "#测试数据的数量\n",
    "mTest = len(testFileList)\n",
    "#从文件中解析出测试集的类别并进行分类测试\n",
    "for i in range(mTest):\n",
    "    #获得文件的名字\n",
    "    fileNameStr = testFileList[i]\n",
    "    #获得分类的数字\n",
    "    classNumber = int(1)\n",
    "    #获得测试集的1x29929向量,用于训练\n",
    "    vectorUnderTest = img2vector('fix-csv-data/test_hc/%s' % (fileNameStr))\n",
    "    #获得预测结果\n",
    "    classifierResult = model.predict(vectorUnderTest)\n",
    "    print(\"文件名为%s\\t分类返回结果为%d\\t真实结果为%d\" % (fileNameStr,classifierResult, classNumber))\n",
    "    if(classifierResult != classNumber):\n",
    "        errorCount += 1.0\n",
    "print(\"总共错了%d个数据\\n错误率为%f%%\" % (errorCount, errorCount/mTest * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
